#pragma once

#include <iostream>
#include <unordered_map>
#include <vector>

#include <fstream> // 文件流头文件

#include "log.hpp"

const std::string default_path = "./recourse/dict.txt";
const std::string sep = " ";

class Translate
{
private:
    std::vector<std::string> vec_line;
    std::unordered_map<std::string, std::string> dictionary;

public:
    Translate()
    {
        LoadDict();
        Init_dict_map();
    }

    ~Translate()
    {
    }

    void LoadDict()
    {
        std::ifstream in(default_path); // 打开文件
        std::string line;

        while (std::getline(in, line)) // 从文件中读取一行
        {
            vec_line.push_back(line); // 放入数组中
        }

        in.close(); // 关闭文件流
        lg.LogMessage(Debug, "Load dictionary success! dictionary path: %s\n", default_path.c_str());
    }

    void Init_dict_map()
    {
        for (auto &e : vec_line)
        {
            size_t it = e.find(sep);

            if (it != std::string::npos)
                dictionary.emplace(e.substr(0, it), e.substr(it + sep.size()));
        }
        lg.LogMessage(Debug, "Init_dict_map success! dictionary path: %s\n", default_path.c_str());
    }

    void DebugPrint()
    {
        for (auto &[first, second] : dictionary)
        {
            std::cout << first << "  " << second << std::endl;
        }
    }

    std::string excute(const std::string &word)
    {
        std::unordered_map<std::string, std::string>::iterator it = dictionary.find(word);
        if (it !=dictionary.end())
        return dictionary[word];
        else
        return "Unknow";
    }
};